package cn.smbms.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 * 封装JPA的工具类
 */
public class JPAUtil {
    private JPAUtil(){}
    private static EntityManagerFactory entityManagerFactory;
    static {
        entityManagerFactory = Persistence.createEntityManagerFactory( "myJpa" );
    }
    //操作数据库的对象
    public static EntityManager getEntityManager(){
        return entityManagerFactory.createEntityManager();
    }
    //关闭entityManager
    public static void close(EntityManager entityManager){
        if (entityManager!=null){
            entityManager.close();
        }
    }
    //回滚事务
    public static void rollback(EntityTransaction transaction){
        if (transaction != null) {
            transaction.rollback();
        }
    }


}
